package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.TudaOrder;
import com.ruoyi.system.domain.TudaOrderTransfer;
import com.ruoyi.system.model.result.OrderTotalAmountResult;
import com.ruoyi.system.service.ITuDaFinanceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CompanyController
 * @Description TODO
 * @Author Admin
 * @Date 2023/8/16 14:56
 * @Version 1.0
 **/
@Controller
@RequestMapping("/system/company/order")
public class CompanyOrderController extends BaseController {

    private String prefix = "system/company/order";

    @Resource
    private ITuDaFinanceService financeService;

    /**
     * 查询财务运单列表
     */
    @RequiresPermissions("system:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TudaOrder tudaOrder, ModelMap mmap)
    {
        startPage();
        tudaOrder.setIsDelete(0);
        List<TudaOrder> list = financeService.queryCompanyOrderListByPhone(tudaOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("system:company:totalAmount")
    @GetMapping("/totalAmount/{id}")
    public String totalAmount(@PathVariable("id") String id, ModelMap mmap)
    {
        OrderTotalAmountResult totalAmountResult = financeService.selectTotalAmount(null);
        mmap.put("freightAmount", totalAmountResult.getFreightAmount());
        return prefix + "/detailed";
    }

    /**
     * 详情
     * @param id
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:company:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = financeService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        if(tudaOrder.getPayType() == 1){
            tudaOrder.setPayTypeValue("到付");
        }else if(tudaOrder.getPayType() == 2){
            tudaOrder.setPayTypeValue("现付");
        }else {
            tudaOrder.setPayTypeValue("欠付");
        }
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/detail";
    }

    /**
     * 运单清单列表
     */
    @RequiresPermissions("system:company:detailed")
    @GetMapping("/detailed/{id}")
    public String detailed(@PathVariable("id") String id, ModelMap mmap)
    {
        String[] str = id.split(",");
        List<TudaOrderTransfer> list = financeService.detailedInfoByHeadPhone(str[0], str[1]);
        BigDecimal totalAmount = new BigDecimal(0.00);
        for (TudaOrderTransfer orderTransfer : list){
            totalAmount = totalAmount.add(orderTransfer.getOwes());
        }
        mmap.put("totalAmount", totalAmount);
        mmap.put("orderTransferList", list);
        return prefix + "/detailed";
    }
}
